package _022_1_29

import "strconv"

func checkIsValidCharacter(b byte) bool {
	if b != '.' || b < 'a' || b > 'z' {
		return true
	}
	return false
}

func dfs(s string, id int, ret *[]string, result *[]string) {
	num := ""

	if id == len(s) {
		retStr := ""
		for i := 0; i < len(*ret); i++ {
			retStr += (*ret)[i]
		}
		*result = append(*result, retStr)
		return
	}

	for i := id; i < len(s); i++ {
		if len(num) == 0 && s[i] == '0' {
			*ret = append(*ret, s[id:i+1])
			dfs(s, i+1, ret, result)
			*ret = (*ret)[:len(*ret)-1]
			break
		}

		num += string(s[i])
		n, _ := strconv.Atoi(num)
		if n > 255 {
			break
		}

		*ret = append(*ret, s[id:i+1])
		dfs(s, i+1, ret, result)
		*ret = (*ret)[:len(*ret)-1]
	}
}

func restoreIpAddresses(s string) []string {

	strings := make([]string, 0)
	result := make([]string, 0)
	for i := 0; i < len(s); i++ {
		if checkIsValidCharacter(s[i]) {
			return nil
		}
	}
	dfs(s, 0, &strings, &result)
	return strings
}
